/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.schema.fetcher;

import com.eng.scc.core.data.DeviceInfo;
import com.eng.scc.core.db.Database;
import com.eng.scc.core.db.Fetcher;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;

/**
 *
 * @author Rafael
 */
public class DeviceInfoFetcher implements Fetcher<DeviceInfo>
{
//    Fields.ID + COMMA +
//    Fields.REF_HARDWARE + COMMA +
//    Fields.INFO + COMMA +
//    Fields.DT_TRANSMITION +
    @Override
    public DeviceInfo fetch( ResultSet ps ) throws Exception
    {
        DeviceInfo h = new DeviceInfo();
        
        h.setId( ps.getInt( 1 ) );
        h.setDeviceId( ps.getInt( 2 ) );
        h.setInfo( ps.getDouble( 3 ) );
        h.setTransmitionDate( new Date( ps.getTimestamp( 4 ).getTime() ) );
        
        return h;
    }

    @Override
    public void insert( DeviceInfo value, PreparedStatement ps ) throws Exception
    {
        value.setId( Database.getInstance().nextID( "core_device_infos" ) );
        
        ps.setInt( 1, value.getId() );
        ps.setInt( 2, value.getDeviceId() );
        ps.setDouble( 3, value.getInfo() );
        ps.setTimestamp( 4, new Timestamp( value.getTransmitionDate().getTime() ) );
    }

    @Override
    public void update( DeviceInfo value, PreparedStatement ps ) throws Exception
    {
        ps.setInt( 1, value.getDeviceId() );
        ps.setDouble( 2, value.getInfo() );
        ps.setTimestamp( 3, new Timestamp( value.getTransmitionDate().getTime() ) );
        ps.setInt( 4, value.getId() );
    }
    
}
